﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Services;


public partial class MatchResultDetails : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    List<MatchSchedule> matchSchedules;
    Result result;
    CricketGeneral crickGen;
    
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (Session["AdminUser"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];
            
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                btnTie.Visible = false;
                btnDraw.Visible = false;
                bindMatch();
                bindTossMatch();
            }
           // bindSessionOvers();
        }
    }

    private void bindTossMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTodayMatcheForClientResult> lstResult = new List<GetTodayMatcheForClientResult>();
        lstResult = matchSchedule.GetTodayMatchesForClient();
        if (lstResult.Count > 0)
        {
            drpTossMatch.DataSource = lstResult;
            drpTossMatch.DataTextField = "Teams";
            drpTossMatch.DataValueField = "Id";
            drpTossMatch.DataBind();
            drpTossMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }

    protected void drpTossMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTeamsByMatchResult> lstResult = new List<GetTeamsByMatchResult>();
        lstResult = matchSchedule.GetTeamsByMatch(Convert.ToInt32(drpTossMatch.SelectedValue));
        if (lstResult.Count > 0)
        {
            drpTossTeam.DataSource = lstResult;
            drpTossTeam.DataTextField = "TeamName";
            drpTossTeam.DataValueField = "Id";
            drpTossTeam.DataBind();
            drpTossTeam.Items.Insert(0, new ListItem("Select Team", "0"));
        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetMatchesForMatchResultResult> lstResult = new List<GetMatchesForMatchResultResult>();
        lstResult = matchSchedule.GetMatchesForMatchResult();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }        
    }
    
    protected void drpMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTeamsByMatchResult> lstResult = new List<GetTeamsByMatchResult>();
        lstResult = matchSchedule.GetTeamsByMatch(Convert.ToInt32(drpMatch.SelectedValue));
        if (lstResult.Count > 0)
        {
            drpTeam.DataSource = lstResult;
            drpTeam.DataTextField = "TeamName";
            drpTeam.DataValueField = "Id";
            drpTeam.DataBind();
            drpTeam.Items.Insert(0, new ListItem("Select Team", "0"));

                       
        }
        List<GetMatchTypeByMatchIdResult> lstMatch = new List<GetMatchTypeByMatchIdResult>();
        lstMatch = matchSchedule.GetMatchTypeByMatchId(Convert.ToInt32(drpMatch.SelectedValue));
        if (lstMatch[0].MatchType.Trim().ToLower() == "one day")
        {
            btnTie.Visible = true;
            btnDraw.Visible = false;
        }
        else
        {
            btnTie.Visible = false;
            btnDraw.Visible = true;
        }
    }
    protected void btnWin_Click(object sender, EventArgs e)
    {
        result = new Result(_dataObject);
        CustomerBetting customerBetting = new CustomerBetting(_dataObject);
        matchSchedule = new MatchSchedule(_dataObject);
        result.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);
        result.WinnerTeam = Convert.ToInt32(drpTeam.SelectedValue);
        result.CreatedDate = System.DateTime.UtcNow;
        result.Create();
        
        List<UpdateTeamARecordsResult> lstA = new List<UpdateTeamARecordsResult>();
        lstA = customerBetting.UpdateTeamARecords(Convert.ToInt32(drpMatch.SelectedValue), Convert.ToInt32(drpTeam.SelectedValue));
        if (lstA.Count > 0)
        {
            customerBetting.UpdateTeamA(Convert.ToInt32(drpMatch.SelectedValue));
        }
        else
        {
            customerBetting.UpdateTeamB(Convert.ToInt32(drpMatch.SelectedValue));
        }


        layerSuccess.InnerHtml = "<strong>Success!</strong> Result added successfully.";
        layerSuccess.Attributes.Add("style", "display:block;");
        clearControl();

    }
    protected void btnTie_Click(object sender, EventArgs e)
    {
        result = new Result(_dataObject);
        result.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);

        CustomerBetting customerBetting = new CustomerBetting(_dataObject);
        customerBetting.UpdateTie(Convert.ToInt32(drpMatch.SelectedValue));      

        result.Tie = true;
        result.CreatedDate = System.DateTime.UtcNow;
        result.Create();
        layerSuccess.InnerHtml = "<strong>Success!</strong> Result added successfully.";
        layerSuccess.Attributes.Add("style", "display:block;");
        clearControl();
    }
    protected void btnDraw_Click(object sender, EventArgs e)
    {
        result = new Result(_dataObject);
        result.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);


        CustomerBetting customerBetting = new CustomerBetting(_dataObject);
        customerBetting.UpdateDraw(Convert.ToInt32(drpMatch.SelectedValue));      
        
        result.Draw = true;
        result.CreatedDate = System.DateTime.UtcNow;
        result.Create();
        layerSuccess.InnerHtml = "<strong>Success!</strong> Result added successfully.";
        layerSuccess.Attributes.Add("style", "display:block;");
        clearControl();
    }

    private void clearControl()
    {
        drpMatch.SelectedIndex = -1;
        drpTeam.SelectedIndex = -1;
        drpTossMatch.SelectedIndex = -1;
        drpTossTeam.SelectedIndex = -1;
    }

    protected void btnTossWinner_Click(object sender, EventArgs e)
    {
        TossWinnerTeam tossWinnerTeam = new TossWinnerTeam(_dataObject);
        tossWinnerTeam.MatchScheduleId = Convert.ToInt32(drpTossMatch.SelectedValue);
        tossWinnerTeam.TeamId = Convert.ToInt32(drpTossTeam.SelectedValue);
        tossWinnerTeam.CreatedDate = System.DateTime.UtcNow;
        tossWinnerTeam.CreateTossWinner();
        layerSuccess.InnerHtml = "<strong>Success!</strong> Result added successfully.";
        layerSuccess.Attributes.Add("style", "display:block;");
        clearControl();
    }
}